const jwt = require('jsonwebtoken')

const whirteList = ['/api/login', '/api/register']
module.exports = () => {
  return async (ctx, next) => {
    if (whirteList.includes(ctx.path)) {
      await next()
    } else {
      const token = ctx.headers;
      try {
        const userInfo = jwt.verify(token, ctx.app.config.keys)
        ctx.state = userInfo;
        await next()
      } catch(err) {
        ctx.body = {
          code: -1,
          msg: 'fail',
          err
        }
      }
    }
  }
}